package od_2025B.one;

import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * todo：
 * 反应了一个问题，对于数据结构的选择及使用，还是不够熟练
 * https://blog.csdn.net/qq_45776114/article/details/147913432
 */
public class Test01 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String firstLine = scanner.nextLine();
        String secondLine = scanner.nextLine();
        scanner.close();

        // 解析第一行，获取流水线数量和作业数量
        String[] strArray1 = firstLine.split(" ");
        int lineCount = Integer.parseInt(strArray1[0]);
        int workCount = Integer.parseInt(strArray1[1]);
        // 解析第二行，获取作业时间
        String[] strArray2 = secondLine.split(" ");
        List<Integer> workHours = new ArrayList<>(workCount);
        for (String str : strArray2) {
            workHours.add(Integer.parseInt(str));
        }
        workHours.sort(Integer::compareTo);
        if (lineCount >= workCount) {
            System.out.println(workHours.get(workHours.size() - 1));
            return;
        }
        // 获取一个固定长度的队列;
        ArrayBlockingQueue<Integer> lineQueue = new ArrayBlockingQueue<>(lineCount);
        // 遍历作业时间
        int maxHour = 0;
        for (Integer workHour : workHours) {
            boolean offer = lineQueue.offer(workHour);
            if (!offer) {
                Integer head = lineQueue.poll();
                maxHour = head + workHour;
                // 由于hour可能有重复，所以，这里 offer的是 maxHour
                lineQueue.offer(maxHour);
            }
        }
        System.out.println(maxHour);
    }
}
